<?php

namespace app\controllers;

use yii\web\Controller;

class Test2Controller extends Controller
{
    // HTTP缓存
    public function behaviors()
    {
        return [
            [
                'class' => 'yii\filters\HttpCache',
                'lastModified' => function() {
                    return filemtime('hw.txt');
                },
                'etagSeed' => function() {
                    $fp = fopen('hw.txt', 'r');
                    $title = fgets($fp);
                    fclose($fp);
                    return $title;
                }
            ]
        ];
    }
    public function actionIndex()
    {
        $content = file_get_contents('hw.txt');
        return $this->renderPartial('index4', ['news' => $content]);
    }
    /*
    // HTTP缓存
    public function behaviors()
    {
        return [
            [
                'class' => 'yii\filters\HttpCache',
                'lastModified' => function() {
                    return 1439690441;
                },
                'etagSeed' => function() {
                    return 'etagseed2';
                }
            ]
        ];
    }
    public function actionIndex()
    {
        return $this->renderPartial('index4');
    }
    */
    /*
    // 页面缓存
    public function behaviors()
    {
        //echo '1';
        return [
            [
                'class' => 'yii\filters\PageCache',
                'duration' => 1000, // 缓存时间
                'only' => ['index', 'text'], // 需要缓存的操作方法
                'dependency' => [ // 缓存依赖
                    'class' => 'yii\caching\FileDependency',
                    'fileName' => 'hw.txt'
                ]
            ]
        ];
    }
    public function actionIndex()
    {
        echo '2';
        //return $this->render('index');
    }
    public function actionTest()
    {
        echo '5';
    }
    */
    /*
    // 片段缓存
    public function actionIndex()
    {
        // 片段缓存
        return $this->renderPartial('index2');
    }
    */
    /*
    // 缓存依赖
    public function actionIndex()
    {
        // 获取缓存组件
        $cache = \Yii::$app->cache;
        // 文件依赖
        //$dependency = new \yii\caching\FileDependency(['fileName' => 'hw.txt']);
        //$cache->add('file_key', 'hello world!', 3000, $dependency);
        //var_dump($cache->get('file_key'));

        //表达式依赖
        //$dependency = new \yii\caching\ExpressionDependency([
        //    'expression' => '\Yii::$app->request->get("name")'
        //]);
        //$cache->add('expression_key', 'hello expression', 3000, $dependency);
        //var_dump($cache->get('expression_key'));

        // DB依赖
        $dependency = new \yii\caching\DbCache([
            'sql' => 'select count(1) from yii.order'
        ]);
        $cache->add('db_key', 'hello DB', 3000, $dependency);
        var_dump($cache->get('db_key'));
    }
    */
    /*
    // 缓存有效期
    public function actionIndex()
    {
        // 获取缓存组件
        $cache = \Yii::$app->cache;
        // 设置有效期
        //$cache->add('key', 'hello world', 15);
        $cache->set('key', 'hello world', 15);

        echo $cache->get('key1');
    }
    */
    /*
    // 缓存 增、删、改、查
    public function actionIndex()
    {
        // 获取缓存组件
        $cache = \Yii::$app->cache;
        // 添加缓存
        $cache->add('key1', 'hello world');
        $cache->add('key2', 'hello world');
        // 获取缓存内容
        $data = $cache->get('key1');
        echo $data, '<br />';
        // 修改缓存内容
        $cache->set('key1', 'hello world2!');
        echo $cache->get('key1');
        // 删除缓存
        $cache->delete('key1');
        var_dump($cache->get('key1'));
        // 清空缓存
        $cache->flush();
        var_dump($cache->get('key2'));
    }
    */
}